//
// VersionPropertiesFile
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep.repo.version;

import java.io.*;
import java.util.*;

/**
 * <p>
 * Wrapper to manage version.properties files.
 * </p>
 */
public class VersionPropertiesFile
{
    // ============================================================
    // Fields
    // ============================================================

    public static final String VERSION_FILE = "version.properties";
    public static final String PROP_VERSION_NUMBER = "version.number";

    private File file;

    // ============================================================
    // Constructors
    // ============================================================

    public VersionPropertiesFile( File file )
    {
        this.file = file;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public String read()
        throws Exception
    {
        // Read file if it really does exist.
        if ( file.exists() )
        {
            Properties props = new Properties();
            FileInputStream in = new FileInputStream( file );
            props.load( in );
            in.close();

            String version = props.getProperty( PROP_VERSION_NUMBER );
            return version == null ? null : version.trim();
        }

        return null;
    }

    public void write( String version, String comments )
        throws IOException
    {
        Properties props = new Properties();
        FileOutputStream out = new FileOutputStream( file );
        props.setProperty( PROP_VERSION_NUMBER, version );
        props.store( out, comments );
        out.close();
    }
}

